package com.ruoyi.web.listener;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.event.DictionaryItemChangeEvent;
import com.ruoyi.web.service.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

/**
 * Description: com.ruoyi.web.listener
 * User: hushibo
 * Date: 2021-05-18 8:59
 * Time: 8:59
 * Version: 1.0
 */
@Configuration
public class EventListenerConfig {

    @Autowired
    private WebSocketServer webSocketServer;

    @EventListener
    public void handleDictionaryItemChange(DictionaryItemChangeEvent evt) {

        Object eventSource = evt.getSource();
        boolean src = eventSource instanceof SysDictData;
        if(src){
            SysDictData data = (SysDictData)eventSource;
            System.out.println("修改的字典项为：" + data.toString());
        } else {
            System.out.println("删除了字典数据：");
        }
        // 处理 字段数据改变事件： 通知前端更新缓存
        webSocketServer.sendMessageToAll(eventSource.toString());
    }
}
